package com.example.beijing.beijingdemo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.beijing.beijingdemo.R;
import com.example.beijing.beijingdemo.activity.MainActivity;
import com.example.beijing.beijingdemo.bean.NewsCenterBean;

import java.util.List;

/**
 * 侧滑菜单Menu的RecyclerView的适配器Adapter
 */

public class MenuAdapter extends RecyclerView.Adapter {

    private Context mContext;

    private List<NewsCenterBean.NewsCenterMenuBean> mDataList;

    // 默认选中的条目
    private int mSelectedPosition;

    /**
     * 外部,设置数据
     */
    public void setmDataList(List<NewsCenterBean.NewsCenterMenuBean> mDataList) {
        this.mDataList = mDataList;
    }

    /**
     * 构造函数
     */
    public MenuAdapter(Context context, List<NewsCenterBean.NewsCenterMenuBean> list) {
        this.mContext = context;
        this.mDataList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.menu_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final NewsCenterBean.NewsCenterMenuBean newsCenterMenuBean = mDataList.get(position);
        MyViewHolder viewHolder = (MyViewHolder) holder;
        viewHolder.tv_title.setText(newsCenterMenuBean.title);

        // 选中
        if (mSelectedPosition == position) {
            viewHolder.iv_arrow.setImageResource(R.drawable.menu_arr_select);
            viewHolder.tv_title.setTextColor(Color.RED);
        } else {
            viewHolder.iv_arrow.setImageResource(R.drawable.menu_arr_normal);
            viewHolder.tv_title.setTextColor(Color.WHITE);
        }

        // 处理条目点击事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedPosition != position) {
                    mSelectedPosition = position;
                    // 刷新界面
                    notifyDataSetChanged();
                    // 修改对应tab的标题
                    ((MainActivity) mContext).getCurrentTabFragment().setTitle(newsCenterMenuBean.title);
                }
                //关闭侧滑菜单
                ((MainActivity) mContext).slidingMenu.toggle();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList != null ? mDataList.size() : 0;
    }

    /**
     * ViewHoder
     */
    private class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_arrow;
        TextView tv_title;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_arrow = (ImageView) itemView.findViewById(R.id.menu_arrow);
            tv_title = (TextView) itemView.findViewById(R.id.menu_title);
        }
    }


}
